package com.welkom.everydate.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.welkom.everydate.common.Constants;
import com.welkom.everydate.domain.Authority;
import com.welkom.everydate.domain.Role;
import com.welkom.everydate.domain.User;
import com.welkom.everydate.services.interfaces.AuthorityService;
import com.welkom.everydate.services.interfaces.RoleService;
import com.welkom.everydate.util.MessageReader;
import com.welkom.everydate.util.web.Operation;
import com.welkom.everydate.util.web.PageData;
import com.welkom.everydate.util.web.PageHeaderAnnotationResolver;
import com.welkom.everydate.util.web.RequestResult;

@Controller
public class RoleManagement {
	
	@RequestMapping("/roleManagement.html")
	public String resourceManagement(Model model){
		return "system/roleManagement";
	}
	@Resource
	private RoleService roleService;
	@Resource
	private AuthorityService authorityService;
	
	@RequestMapping("/roleQuery.html")
	@ResponseBody
	public PageData<Role> queryRolesByPage(@RequestParam(required=true,value="pageNum")int pageNum,@RequestParam(required=true,value="pageSize")int pageSize, @RequestParam(required=true,value="roleJsonString") String roleJsonString){
		JSONArray array = JSONArray.fromObject(roleJsonString);//先读取串数组
		 Object[] objectArray = array.toArray();  
		 while(objectArray[0] instanceof JSONArray){
			 JSONArray currentArray=(JSONArray)objectArray[0];
			 Object[] currentObjectArray=currentArray.toArray();
			 objectArray=currentObjectArray;
		 }
		 if(pageSize==0){
				pageSize=Constants.Pagination.DEFAULT_PAGE_SIZE;
		 }
		 if(pageNum==0){
			 pageNum=1;
		 }
		 
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 Role role=(Role)obj.toBean(obj, Role.class);
		 int totalCount=roleService.getTotalCountByQueryCondition(role);
		 PageData<Role> page=new PageData<Role>();
		 page.setPageSize(pageSize);
		 page.setCurrentPage(pageNum);
		 page.setTotalCount(totalCount);
		 if(page.getPageNavigatorLength()==0){
			 page.setPageNavigatorLength(Constants.Pagination.DEFAULT_PAGE_NAVIGATOR_LENGTH);
		 }
		 page.setPageHeaders(PageHeaderAnnotationResolver.getHeadersFromDomain(Role.class));
		 List<Operation> operations=new ArrayList<Operation>();
		 page.setOperations(operations);
		 int startIndex=page.getStartIndex();
		 List<Role> roles=roleService.getRolesByPage(role, startIndex, pageSize);
		 page.setData(roles);
		return page;
	}
	
	
	@RequestMapping("/getRoleDetail.html")
	@ResponseBody
	public RequestResult<Role> getRoleDetail(@RequestParam(required=true,value="roleId")long roleId){
		RequestResult<Role> result=new RequestResult<Role>();
		Role role=roleService.getRoleById(roleId);
		if(role==null){
			result.setResultCode(Constants.ResultCode.RESULT_CODE_FAIL);
			result.setReason(MessageReader.getMessage(Constants.Message.MESSAGE_OBJECT_NOT_EXSIT));
		}else{
			result.setData(role);
			result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		}
		return result;
	}
	@RequestMapping("/saveChangesToRole.html")
	@ResponseBody
	public RequestResult<Role> saveChangesToRole(@RequestParam(required=true,value="roleJsonString")String roleJsonString){
		RequestResult<Role> result=new RequestResult<Role>();
		JSONArray array = JSONArray.fromObject(roleJsonString);//先读取串数组
		 Object[] objectArray = array.toArray();  
		 while(objectArray[0] instanceof JSONArray){
			 JSONArray currentArray=(JSONArray)objectArray[0];
			 Object[] currentObjectArray=currentArray.toArray();
			 objectArray=currentObjectArray;
		 }
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 Role role=(Role)obj.toBean(obj, Role.class);
		 roleService.saveChangesToRole(role);
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 result.setData(role);
		 return result;
	}
	@RequestMapping("/addRole.html")
	@ResponseBody
	public RequestResult<Role> addRole(@RequestParam(required=true,value="roleJsonString")String roleJsonString){
		RequestResult<Role> result=new RequestResult<Role>();
		JSONArray array = JSONArray.fromObject(roleJsonString);//先读取串数组
		 Object[] objectArray = array.toArray();  
		 while(objectArray[0] instanceof JSONArray){
			 JSONArray currentArray=(JSONArray)objectArray[0];
			 Object[] currentObjectArray=currentArray.toArray();
			 objectArray=currentObjectArray;
		 }
		 JSONObject obj = JSONObject.fromObject(objectArray[0]);
		 Role role=(Role)obj.toBean(obj, Role.class);
		 roleService.addRole(role);
		 result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		 result.setData(role);
		 return result;
	}
	
	@RequestMapping("/deleteRoleById.html")
	@ResponseBody
	public RequestResult<Role> deleteRoleById(@RequestParam(required=true,value="roleId")long roleId){
		roleService.deleteRoleById(roleId);
		RequestResult<Role> result=new RequestResult<Role>();
		result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		return result;
	}
	
	
	@RequestMapping("/getCurrentRoleAuthorities.html")
	@ResponseBody
	public RequestResult<Map> getCurrentRoleAuthorities(@RequestParam(required=true,value="roleId")long roleId){
		RequestResult<Map> result=new RequestResult<Map>();
		List<Authority> allAuthorities=authorityService.getAllAuthoritys();
		List<Authority> currentAuthorities=authorityService.getAuthoritiesByRoleId(roleId);
		List<Authority> otherAuthorities=new ArrayList<Authority>();
		for(Authority authority:allAuthorities){
			if(!currentAuthorities.contains(authority)){
				otherAuthorities.add(authority);
			}
		}
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("currentAuthorities", currentAuthorities);
		map.put("otherAuthorities", otherAuthorities);
		result.setData(map);
		result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		return result;
	}
	
	@RequestMapping("/saveAuthorityChangesToRole.html")
	@ResponseBody
	public RequestResult<Role> saveAuthorityChangesToRole(@RequestParam(required=true,value="authorityJsonString")String authorityJsonString,@RequestParam(required=true,value="roleId")long roleId){
		JSONArray array = JSONArray.fromObject(authorityJsonString);//先读取串数组
		Object[] objectArray = array.toArray();  
		List<Authority> authorities=new ArrayList<Authority>();
		for(int i=0;i<objectArray.length;i++){
			JSONObject obj = JSONObject.fromObject(objectArray[i]);
			Authority authority=(Authority)obj.toBean(obj, Authority.class);
			authorities.add(authority);
		}
		authorityService.removeAuthoritiesByRoleId(roleId);
		authorityService.addAuthoritiesToRole(authorities, roleId);
		RequestResult<Role> result=new RequestResult<Role>();
		result.setResultCode(Constants.ResultCode.RESULT_CODE_SUCCESS);
		return result;
	}
	
	
}
